package com.itheima.service.ums.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.core.model.AjaxResult;
import com.itheima.common.utils.JwtUtils;
import com.itheima.mapper.ums.AdminMapper;
import com.itheima.pojo.ums.UmsAdminLoginParam;
import com.itheima.pojo.ums.UmsMenu;
import com.itheima.pojo.ums.UmsRole;
import com.itheima.service.ums.AdminService;
import com.itheima.service.ums.RoleService;
import com.itheima.service.ums.UmsMenuService;



import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class AdminServiceImpl extends ServiceImpl<AdminMapper, UmsAdminLoginParam> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UmsMenuService umsMenuService;
    @Autowired
    private RoleService roleService;

    @Override
    public AjaxResult login(UmsAdminLoginParam umsAdmin) {
      LambdaQueryWrapper<UmsAdminLoginParam> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsAdminLoginParam::getUsername,umsAdmin.getUsername());
        UmsAdminLoginParam u = this.getOne(lqw);
        if(u == null){
            return AjaxResult.error("用户名或者密码错误");
        }
        if (u.getStatus()!=1){
            return AjaxResult.error("该账号已经被禁用！");
        }
       /* String password = BCryptUtil.getPassword(u.getPassword());
        if (!BCryptUtil.checkPassword(umsAdmin.getPassword(),password)){
            return ResultData.error("密码错误!");
        }*/
        Map hashmap=new HashMap();
        hashmap.put("username",u.getUsername());
        hashmap.put("id",u.getId());
        String token = JwtUtils.getToken(hashmap);
        /*String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ6eGciLCJjcmVhdGVkIjoxNjU3MDE0MDQ4MTgwLCJleHAiOjE2NTc2MTg4NDh9.ZEi1hvugsdSM4E918y5b_JF05KfK0nmxOyGJahEUGw8gnLznGRB1L6Cy39CFz3VC7gWqbwwtLv1oSB1-rr5KMA";*/
        String tokenHead = "Bearer ";
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("tokenHead",tokenHead);
        return AjaxResult.success("操作成功",map);
    }
    @Override
    public HashMap<String, Object> info() {
        HashMap<String, Object> map = new HashMap<>();

        UmsAdminLoginParam user = this.getById(3);
        map.put("icon",user.getIcon());
        map.put("username",user.getUsername());

        List<Integer> roleIds = adminMapper.getRoleIds(user.getId());
        LambdaQueryWrapper<UmsRole> roleLQW = new LambdaQueryWrapper<>();
        roleLQW.in(UmsRole::getId,roleIds);
        List<UmsRole> roles = roleService.list(roleLQW);
        List<String> list = new ArrayList<>();
        for (UmsRole role : roles) {
            list.add(role.getName());
        }
        map.put("roles","超级管理员");

        List<Integer> menuIds = adminMapper.getMenuIds(roleIds);
        LambdaQueryWrapper<UmsMenu> menuLQW = new LambdaQueryWrapper<>();
        menuLQW.in(UmsMenu::getId,menuIds);
        List<UmsMenu> menus =umsMenuService.list();
        map.put("menus",menus);

        return map;
    }

}
